<?php
    class User extends CI_Model{
       
        function login($username,$password){
            $this->db->select('id,username,password');
            $this->db->from ('user');
            $this->db->where ('username',$username);
            $this->db->where('password',MD5($password));
            $this->db->limit (1);
            
            $query = $this->db->get();
            
            if($query->num_rows() > 0)
            {
                return TRUE;
            }
            else    
            {
                return FALSE;
            }
        }
        
        function exist_user($username,$email)
        {
            $this->db->select('username');
            $this->db->from ('user');
            $this->db->where ('username',$username);
            $this->db->or_where('email',$email);
            $this->db->limit (1);
            
            $query = $this->db->get();
            
            if($query->num_rows() == 0)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }
        
        function add_user($username,$email,$password,$creation_date,$language,$ip)
        {
            $sql_query = 'INSERT INTO `user`(`id`, `username`, `email`, `password`, `creationdate`, `language`, `ip`)
                                       VALUES (?,?,?,MD5(?),?,?,?)';
            $query = $this->db->query($sql_query,array('',$username,$email,$password,$creation_date,$language,$ip));
            
            $affected_rows = $this->db->affected_rows();
            
            if($affected_rows == 1)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }
    }
?>
